package com.fgxia.admin.controller.operation;

import com.fgxia.admin.service.IAdminSiteService;
import com.ruoyi.business.domain.SiteInfo;
import com.ruoyi.business.domain.bo.SiteInfoBo;
import com.ruoyi.business.domain.vo.SiteInfoVo;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.core.validate.Type1Group;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * 地址信息
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/operation/site")
public class AdminSiteController extends BaseController {

    private final IAdminSiteService siteInfoService;

    @GetMapping("/list")
    public TableDataInfo<SiteInfoVo> list(SiteInfo siteInfo, PageQuery pageQuery) {
        siteInfo.setRelevanceUserId(getUserId());
        return siteInfoService.selectVoList(siteInfo, pageQuery);
    }

    @RepeatSubmit
    @PostMapping()
    public R<Void> add(@Validated({AddGroup.class, Type1Group.class}) @RequestBody SiteInfoBo siteInfoBo) {
        siteInfoBo.setRelevanceUserId(getUserId());
        Boolean result = siteInfoService.insertByBo(siteInfoBo);
        return toAjax(result);
    }

    @RepeatSubmit
    @PutMapping()
    public R<Void> update(@Validated({EditGroup.class, Type1Group.class}) @RequestBody SiteInfoBo siteInfoBo) {
        Boolean result = siteInfoService.updateByBo(siteInfoBo);
        return toAjax(result);
    }

    /**
     * 设为默认
     */
    @RepeatSubmit
    @PutMapping("/default/{siteId}")
    public R<Void> setDefault(@PathVariable("siteId") Long siteId) {
        Boolean result = siteInfoService.setDefault(siteId, getUserId());
        return toAjax(result);
    }

    /**
     * 获取默认地址
     */
    @GetMapping("/default")
    public R<SiteInfoVo> getDefault() {
        return R.ok(siteInfoService.selectDefault(getUserId()));
    }

    @RepeatSubmit
    @DeleteMapping("/{siteId}")
    public R<Void> delete(@PathVariable Long siteId) {
        Boolean result = siteInfoService.deleteById(siteId);
        return toAjax(result);
    }

}
